var WxParse = require('../../../wxParse/wxParse.js');
var goods_table = require('../../../template/goodsTable/goods_table')
var app = getApp();

Page({
  data: {
    goods: {},
    current: 1,
    openId: '',
    cartCount: 0,
    isCollect: false,
    txt: "收藏",
    isLoading: true,
    indicatorDots: false
  },
  currentchange(e) {
    this.setData({
      current: e.detail.current + 1
    });
  },
  toLogin: function (e) {
    // wx.showModal({
    //   title: '温馨提示',
    //   content: '您尚未登录成为会员,是否立即登录？',
    //   success(res) {
    //     if (res.confirm) {
    //       wx.switchTab({
    //         url: '../../user/user'
    //       });
    //     } else if (res.cancel) {}
    //   }
    // })


  },
  addCart: function (e) {
    //currentDate = app.utils.formatDate(Date.now(), 'yyyy年mm月dd日 HH时MM分ss秒');
    var that = this;
    var cart = [];
    wx.getStorage({
      key: 'cart',
      complete(res) {
        if (res.data) {
          cart = res.data;
          let index = cart.findIndex(c => c.id === that.data.goods.id);
          if (index > -1) //已经存在
          {
            var count = cart[index].count;
            cart[index].count = count + 1;
          } else {
            var goods = {
              'id': that.data.goods.id,
              'title': that.data.goods.title,
              'src': that.data.goods.src,
              'price': that.data.goods.price,
              'count': 1,
              'selected':false
            };
            cart.push(goods);
          }
          var count = 0;
          cart.forEach(function (item) {
            count = count + item.count;
          });
          that.setData({
            cartCount: count,
            data: cart
          });
        } else {
          var goods = {
            'id': that.data.goods.id,
            'title': that.data.goods.title,
            'src': that.data.goods.src,
            'price': that.data.goods.price,
            'selected':true,
            'count': 1
          };
          cart.push(goods);
        }
        wx.setStorage({
          key: "cart",
          data: cart
        });
      }
    });
    var count = that.data.cartCount;
    that.setData({
      cartCount: count + 1
    });
  },
  //添加收藏
  addCollect: function (e) {
    var that = this;
    that.setData({
      txt: "已收藏",
      isCollect: true
    });
  },
  /*----------------生命周期函数-----------------------------*/
  onLoad(options) {
    var that = this;
    wx.getStorage({
      key: 'cart',
      complete(res) {
        if (res.data) {
          var count = 0;
          res.data.forEach(function (item) {
            count = count + item.count;
          });
          that.setData({
            cartCount: count
          });
        }
      }
    });
    var article = `
	<div>
			<video src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400"></video>
		</div>
	<!--ap-->`;
    /**********************************************/

    var goods_item = goods_table.getGoodsItem(options.id)[0];
    that.setData({
      goods: goods_item
    });
    //article = res.data[0].Introduce + article;
    WxParse.wxParse('article', 'html', article, that, 5);

    that.loading = this.selectComponent("#loading");
    that.loading.HideLoading();
    /**
     * html解析示例
     */
    //   var article = `< !DOCTYPE HTML ><!--注释: wxParse试验文本-->
    // <div style="margin-top:10px;">
    // 	<div style="margin-top:10px;">
    // 		<video src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400"></video>
    // 	</div>
    // </div>
    // <div style="margin-top:10px;">
    // 	<div style="margin-top:10px;">
    // 		<p>[00][01][02][03][04][05][06][07][08][09]</p>
    // 	</div>
    // </div>
    // <div style="margin-top:10px;">
    // 	<div style="margin-top:10px;">
    //   <img src="https://img10.360buyimg.com/imgzone/jfs/t1/97055/32/14924/274214/5e6b206fE0af147b2/160bde8c14aee239.jpg" />
    // 		<img src="http://a.hiphotos.baidu.com/image/pic/item/9a504fc2d5628535959cf4cf94ef76c6a6ef63db.jpg" alt="">
    // 		<img src="http://e.hiphotos.baidu.com/image/pic/item/48540923dd54564e1e1ac2d7b7de9c82d0584fe4.jpg" alt="">
    // 	</div>
    // </div>
    // <!--ap-->`;
    /**********************************************/
  },
  onShareAppMessage(res) {
    console.log(res);
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '老铁,我在商城发现了一个好宝贝,一起看看吧',
      path: 'views/goods/detail/detail?id'
    }
    // return {
    //   title: '商品页',
    //   desc: this.data.product.goods_name,
    //   path: '/products/products?id=' + this.data.product.id
    // }
  },



  navigateToCart() {
    wx.switchTab({
      url: '../../cart/cart'
    });
  }
});